//
//  LTQCycleImages.m
//  UI06_CycleImages
//
//  Created by Hanlingyu on 16/7/21.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "LTQCycleImages.h"
#define KScreenBounds [UIScreen mainScreen].bounds
#define kScreenWidth KScreenBounds.size.width
#define kScreenHeight KScreenBounds.size.height

// 写属性用延展
@interface LTQCycleImages ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *imagesPage;
@property (nonatomic, strong) UIScrollView *cycleScrollView;

@end

@implementation LTQCycleImages
bool a;
#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // 子控件初始化时, frame不要与初始化方法的frame一样
        // 推荐使用self.bounds / CGRectMack(0, 0, frame.size.width, frame.size.height)
        
        [self initCycleScrollView];
        [self initImagePage:frame];
    }
    return self;
}
- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self initCycleScrollView];
    [self initImagePage:self.frame];
    
}

- (void)initCycleScrollView {

    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _cycleScrollView.delegate = self;
    [self addSubview:_cycleScrollView];
}

//#error error
//#warning 后期需要添加page的位置改变, 这里默认位置

// 后期需要添加page的位置改变, 这里默认位置
- (void)initImagePage:(CGRect)frame {
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _imagesPage.backgroundColor = [UIColor clearColor];
//    _imagesPage.numberOfPages = 5;
    [self addSubview:_imagesPage];
    _imagesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - 30);
//    [_imagesPage addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
//    [self.imagesPage addGestureRecognizer:tap];
    
}
// 问题: 如何通过pageControl进行轮播
//- (void)imagePageChange:(UIPageControl *)pageControl {
//    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
//    
//    if (pageControl.currentPage == 4) {
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
//        [pageControl addGestureRecognizer:tap];
//        
//    }
//
//}


- (void)nextPage:(UITapGestureRecognizer *)tap {
    CGPoint a = [tap locationInView:self.imagesPage];
//    NSLog(@"dsfasdf");
    if (a.x > self.imagesPage.frame.size.width / 2) {
        [UIView animateWithDuration:0.5 animations:^{
            
            _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
            
        }];
        
        _imagesPage.currentPage = 0;
    } else if (a.x < self.imagesPage.frame.size.width / 2) {
        [_cycleScrollView setContentOffset:CGPointMake((_imagesPage.currentPage - 1) * self.bounds.size.width, 0) animated:YES];
    
    
    }
    
    
    [_imagesPage removeGestureRecognizer:tap];

}






#pragma mark - ImageArray
- (void)setImagesWithArray:(NSArray *)array {
    // 1. 根据传过来的数组, 进行处理, 生成图片数组
    NSArray *imagesArray = [self handleArray:array];
    // 2. 逻辑判断时, 需要用到数组内容
    self.imageArray = [NSArray arrayWithArray:imagesArray];
    // 3. 根据数组生成对应的ImageView
    [self createAllImageViews:imagesArray];
    _imagesPage.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(self.bounds.size.width * imagesArray.count, 0);
    // 因为添加了最后一张, 要让轮播图看起来在第一张
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}

// 使用数组创建ImageView
- (void)createAllImageViews:(NSArray *)imagesArray {
    // 逻辑判断用到数组属性
    // 直接使用数组创建imageView
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}


- (NSArray *)handleArray:(NSArray *)array {
    // 判断类型(不知道别人使用你的类的时候传进来的类型是什么)
    id firstObject = [array firstObject];
    // 最终的图片数组
    NSMutableArray *imagesArray = [NSMutableArray array];
    // 判断对象是不是某种类型 isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串转成 UIImgae数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        // 把数组等于可变数组
        [imagesArray setArray:array];
    }
    // 获取到第一张图片
    UIImage *firstImage = [imagesArray firstObject];
    // 获取到最后一张图片
    UIImage *lastImage = [imagesArray lastObject];
    // 下标为0的位置放置最后一张图片
    [imagesArray insertObject:lastImage atIndex:0];
    // 在最后添加第一张图
    [imagesArray addObject:firstImage];
    // 把可变数组copy一份就是不可变的
    return [imagesArray copy];
}

#pragma mark - 协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 高边PageControl的对应下标
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagesPage.currentPage = page - 1;
    // 改变滚动视图的偏移量
    if (page == 0 ) {
        _cycleScrollView.contentOffset = CGPointMake((self.imageArray.count - 2) * self.bounds.size.width, 0);
        _imagesPage.currentPage = _imageArray.count - 1 - 2;
    } else if (page == self.imageArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPage.currentPage = 0;
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
